package com.xy.xylive.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.xy.xylive.api.req.GetLiveReq;
import com.xy.xylive.api.res.CreateLiveRes;
import com.xy.xylive.common.ServiceException;
import com.xy.xylive.config.XylinkConf;
import com.xy.xylive.aop.NoSign;
import com.xy.xylive.api.req.CreateLiveReq;
import com.xy.xylive.xyapi.LiveVideoReq;
import com.xy.xylive.xyapi.LiveVideoRes;
import com.xy.xylive.xyapi.LiveVideoV5Api;
import com.xylink.config.SDKConfigMgr;
import com.xylink.model.SdkCloudMeetingRoomRequest;
import com.xylink.model.SdkMeeting;
import com.xylink.sdk.conferenceControl.CreateMeetingApi;
import com.xylink.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/")
@Slf4j
public class ApiController {

    @Autowired
    private XylinkConf xylinkConf;

    @RequestMapping
    @NoSign
    public String welcome() {
        return "Welcome!";
    }

    @RequestMapping("/createLive")
    public R<CreateLiveRes> createLive(@RequestBody CreateLiveReq createLiveReq) throws IOException {
        if(StrUtil.isEmpty(createLiveReq.getTitle())) {
            throw new ServiceException("缺少必要参数");
        }
        String enterpriseId = xylinkConf.getEnterpriseId();

        String meetingNumber = createLiveReq.getMeetingNumber();
        if(StrUtil.isEmpty(createLiveReq.getMeetingNumber())) {
            SdkCloudMeetingRoomRequest sdkCloudMeetingRoomRequest = new SdkCloudMeetingRoomRequest();
            sdkCloudMeetingRoomRequest.setMeetingName(createLiveReq.getTitle());
            CreateMeetingApi createMeetingApi = new CreateMeetingApi();
            SdkMeeting sdkMeeting = createMeetingApi.createMeetingV2(enterpriseId, SDKConfigMgr.getAccessToken(), sdkCloudMeetingRoomRequest);
            log.info(sdkMeeting.toString());
            meetingNumber = sdkMeeting.getMeetingNumber();
        }


        LiveVideoReq liveVideo = new LiveVideoReq();
        liveVideo.setTitle(createLiveReq.getTitle());
        liveVideo.setStartTime(DateUtil.offsetSecond(DateUtil.date(), 3).getTime());
        liveVideo.setEndTime(DateUtil.offsetMinute(DateUtil.date(), 30).getTime());
        LiveVideoV5Api liveVideoV5Api = new LiveVideoV5Api();
        Result<LiveVideoRes> liveVideoForConferenceResult = liveVideoV5Api.scheduleLive(enterpriseId, SDKConfigMgr.getAccessToken(), meetingNumber, liveVideo);
        if(!liveVideoForConferenceResult.isSuccess()) {
            throw new ServiceException("创建直播失败 " + liveVideoForConferenceResult.getErrorMsg());
        }
        LiveVideoRes liveVideoRes = liveVideoForConferenceResult.getData();
        log.info(liveVideoRes.toString());

        CreateLiveRes createLiveRes = new CreateLiveRes();
        createLiveRes.setMeetingNumber(meetingNumber);
        createLiveRes.setLiveId(liveVideoRes.getLiveId());
        createLiveRes.setPushUrl(liveVideoRes.getPushUrl());
        createLiveRes.setPullUrls(liveVideoRes.getPullUrls());
        createLiveRes.setTitle(liveVideoRes.getTitle());
        return R.ok(createLiveRes);
    }

    @RequestMapping("/getLive")
    public R<LiveVideoRes> getLive(GetLiveReq getLiveReq) throws IOException {
        if(StrUtil.hasBlank(getLiveReq.getLiveId(), getLiveReq.getMeetingNumber())) {
            throw new ServiceException("缺少必要参数");
        }
        String enterpriseId = xylinkConf.getEnterpriseId();
        LiveVideoV5Api liveVideoV5Api = new LiveVideoV5Api();
        Result<LiveVideoRes> liveVideoResResult = liveVideoV5Api.getLive(enterpriseId, SDKConfigMgr.getAccessToken(), getLiveReq.getLiveId(), getLiveReq.getMeetingNumber(), getLiveReq.getPassword());
        if(!liveVideoResResult.isSuccess()) {
            throw new ServiceException("查询直播失败 " + liveVideoResResult.getErrorMsg());
        }
        return R.ok(liveVideoResResult.getData());
    }


}
